package com.hyk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.Account;
import com.hyk.dto.AccountDto;
import com.hyk.dto.R;
import com.hyk.feign.AccountFeign;
import com.hyk.model.SymbolAssetVo;
import com.hyk.model.UserTotalAccountVo;
import com.hyk.service.AccountService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@Api(tags = "账户控制器")
@RequestMapping("/account")
public class AccountController implements AccountFeign {

    @Resource
    private AccountService accountService;

    @GetMapping("/{coinName}")
    public R<Account> getUserAccount(@PathVariable(value = "coinName") String coinName){
        Long userId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Account account = accountService.findUserAccountByCoinName(null,coinName,userId);
        return R.ok(account);
    }

    @GetMapping
    public R<Page<Account>> list(Page<Account> page,
                                 Long userId,
                                 String mobile,
                                 String userName,
                                 Long coinId,
                                 String startAmount,
                                 String endAmount,
                                 Byte status){
        Page<Account> page1 = accountService.findPageForList(page,userId,userName,mobile,coinId,startAmount,endAmount,status);
        return R.ok(page1);
    }

    @GetMapping("/asset/{symbol}")
    public R<SymbolAssetVo> getSymbolAccount(@PathVariable String symbol){
        SymbolAssetVo symbolAssetVo = accountService.findSymbolAccount(symbol);
        return R.ok(symbolAssetVo);
    }

    @GetMapping("/total")
    public R<UserTotalAccountVo> total(){
        UserTotalAccountVo accounts = accountService.findTotal();
        return R.ok(accounts);
    }

    @Override
    @GetMapping("/getUserAccountByCoinId")
    public AccountDto getUserAccountByCoinId(Long coinId, Long userId) {
        Account account = accountService.findUserAccountByCoinName(coinId, null, userId);
        AccountDto accountDto = new AccountDto();
        BeanUtils.copyProperties(account,accountDto);
        return accountDto;
    }

    @Override
    @GetMapping("/transferUserLock")
    public Boolean transferUserLock(Long coinId, Long userId, BigDecimal amount, Long orderId) {
        return accountService.transferUserLock(coinId,userId,amount,orderId);
    }
}
